package com.loong.web.gp4j.view.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.loong.web.gp4j.bean.param.attach.AttachParam;
import com.loong.web.gp4j.bean.schema.user.Auths;
import com.loong.web.gp4j.service.attach.AttachService;
import com.loong.web.gp4j.service.error.ErrorService;
import com.loong.web.gp4j.service.monitor.MonitorService;
import com.loong.web.suppert.collection.vo.ResultVo;
import com.loong.web.suppert.intercept.auth.AuthType;
import com.loong.web.suppert.intercept.statistic.Statistic;

/**
 * 系统基础资源
 *
 * @author 张成轩
 */
@Controller
public class UtilViewMapping {

	@Resource
	private AttachService attachService;
	@Resource
	private ErrorService errorService;
	@Resource
	private MonitorService monitorService;

	/**
	 * 关于页面
	 * 
	 * @param request 请求
	 * @return 视图
	 */
	@RequestMapping(value = "about", method = RequestMethod.GET)
	@AuthType
	@Statistic("关于")
	public ModelAndView about(HttpServletRequest request) {

		return new ModelAndView("main/about");
	}

	/**
	 * 错误页面
	 * 
	 * @param request 请求
	 * @param code 错误代码
	 * @return 视图
	 */
	@RequestMapping(value = "error/{code}", method = RequestMethod.GET)
	@AuthType
	public ModelAndView error(HttpServletRequest request, @PathVariable String code) {

		errorService.error(request, code);

		ModelAndView model = new ModelAndView("main/error");
		model.addObject("code", code);
		return model;
	}

	/**
	 * 获取附件
	 * 
	 * @param response 响应
	 * @param param 参数
	 * @param valid 校验器
	 */
	@RequestMapping(value = "attach", method = RequestMethod.GET)
	@ResponseBody
	@AuthType
	public void attach(HttpServletResponse response, @Valid AttachParam param, BindingResult valid) {

		if (valid.hasErrors())
			return;
		attachService.get(response, param);
	}

	/**
	 * 文件上传
	 * 
	 * @param request 请求
	 * @param file 文件对象
	 * @param c 是否压缩
	 * @return 返回对象
	 */
	@RequestMapping(value = "attach/upload", method = RequestMethod.POST)
	@ResponseBody
	@AuthType(Auths.EDITOR)
	public ResultVo<?> upload(HttpServletRequest request, MultipartFile file, Boolean c) {

		return attachService.upload(file, c);
	}
}
